<style>
</style>
<?php
include dirname(__FILE__) . '/panel.php';
?>
<script type="text/javascript">
	var container, stats;

	var camera, scene, renderer;

	var cube, plane;
	var stageSize;

	var targetRotation = 0;
	var targetRotationOnMouseDown = 0;

	var mouseX = 0;
	var mouseXOnMouseDown = 0;

	var clock = new THREE.Clock();

	var windowHalfX = window.innerWidth / 2;
	var windowHalfY = window.innerHeight / 2;
	var mouse2D = new THREE.Vector3(0, 10000, 0.5);
	var house;
	var mouseX = 0, mouseY = 0;
	init();
	animate();

	function init() {
		container = document.createElement('div');
		document.body.appendChild(container);
		lon = -90;
		scene = new THREE.Scene();
		camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 100000);
		// camera = new THREE.CombinedCamera( window.innerWidth, window.innerHeight, 45, 1, 10000, -2000, 10000 );
		camera.orgPosition=new THREE.Vector3(0,100,500);
		camera.position.y = 100;
		camera.position.z = 500;
		// camera.position.y = 500;
		scene.add(camera);
		controls = new FirstPersonControls(camera);
		controls.lookSpeed = 0.0125;
		controls.movementSpeed = 500;
		controls.noFly = false;
		controls.lookVertical = true;
		controls.constrainVertical = false;
		// controls.verticalMin = 1.5;
		// controls.verticalMax = 2.0;
		controls.lon = lon;
		controls.freeze = true;

		// LIGHTS

		var ambient = new THREE.AmbientLight(0x221100);
		scene.add(ambient);

		var directionalLight = new THREE.DirectionalLight(0xffffff);
		directionalLight.position.set( 70, 70, 100 ).normalize();
		scene.add(directionalLight);

		var directionalLight = new THREE.DirectionalLight(0xffffff);
		directionalLight.position.set( -70, -70, -100 ).normalize();
		scene.add(directionalLight);
		// Cube
		
		var materials = [];
		
		for ( var i = 0; i < 6; i ++ ) {
		
		materials.push( new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } ) );
		
		}
		
		cube = new THREE.Mesh( new THREE.CubeGeometry( 200, 200, 200, 1, 1, 1, materials ), new THREE.MeshFaceMaterial() );
		cube.position.y = 150;
		scene.add( cube );
		
		// Plane
		
		plane = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
		plane.rotation.x = - 90 * ( Math.PI / 180 );
		scene.add( plane );

		stageSize = {
			width : window.innerWidth,
			height : window.innerHeight
		};

		// facility=new Facility(scene,camera,stageSize);
		// var cursorGeo = new THREE.CubeGeometry( 500, 1000, 10 );
		// cursorGeo.computeBoundingBox();
		// var cursorMaterial = new THREE.MeshBasicMaterial( { color: 0xff00aa, opacity: 0.5, transparent: true } );
		// // camera =addMesh(cursorGeo, 1, camera.position.x, camera.position.y, camera.position.z, 0, 0, 0, cursorMaterial, 'cursor');
		// // geometry, scale, position, rx, ry, rz, material, isFocused,  name, description, _id,gTypeId
		// facility.loadStyle(cursorGeo,1,new THREE.Vector3(0,0,0),0,0,0,cursorMaterial, true,'wall','description','_id','fwall');

		renderer = new THREE.WebGLRenderer();
		 // renderer.setSize(window.innerWidth, window.innerHeight-50);
		renderer.setSize(window.innerWidth, 650);

		container.appendChild(renderer.domElement);
		// document.addEventListener('mousedown', getFacility, false);
		$('canvas').bind('click', function(event) {getFacility(event)
		});
	}

	function cameraInsector() {
		if(controls.rotateRight || controls.rotateLeft)
			return;
		var f_vector = new THREE.Vector3(0, 0, -1);
		var b_vector = new THREE.Vector3(0, 0, 1);
		var l_vector = new THREE.Vector3(-1, 0, 0);
		var r_vector = new THREE.Vector3(1, 0, 0);
		halfSize = 10;
		var left = new THREE.Vector3(camera.position.x - halfSize, camera.position.y - halfSize, camera.position.z);
		var right = new THREE.Vector3(camera.position.x + halfSize, camera.position.y - halfSize, camera.position.z);
		var front = new THREE.Vector3(camera.position.x, camera.position.y - halfSize, camera.position.z + halfSize);
		var back = new THREE.Vector3(camera.position.x, camera.position.y - halfSize, camera.position.z - halfSize);

		// front
		// if(controls.moveForward) {
		var left_ray = new THREE.Ray(left, f_vector);
		var left_intersects = left_ray.intersectObjects(scene.children);
		if(left_intersects.length > 0 && left_intersects[0].distance < halfSize) {
			camera.position.z = left_intersects[0].point.z + halfSize + 1;
			controls.moveForwardFreeze = true;
			controls.freeze = true;
			// if(left_intersects[0].object.gTypeId!='10'){
				// controls.moveForwardFreeze = true;
				// controls.freeze = true;
			// }
			// else{
				// controls.freeze = false;
				// controls.moveForwardFreeze = false;
				// if(this.camera.position.z>=left_intersects[0].object.position.z)
					// this.camera.position.z-=40;
				// else
					// this.camera.position.z+=40;
			// }
			console.warn("intersected on	" + left_intersects[0].object.name + " " + left_intersects[0].object.gTypeId + " " + left_intersects[0].object.description);
			return;
			//alert(left_intersects[0].point.z);

		}
		var right_ray = new THREE.Ray(right, f_vector);
		var right_intersects = right_ray.intersectObjects(scene.children);
		if(right_intersects.length > 0 && right_intersects[0].distance < halfSize) {
			camera.position.z = right_intersects[0].point.z + halfSize + 1;
			controls.freeze = true;
			controls.moveForwardFreeze = true;
			// if(left_intersects[0].object.gTypeId!='10'){
				// controls.moveForwardFreeze = true;
				// controls.freeze = true;
			// }
			// else{
				// controls.freeze = false;
				// controls.moveForwardFreeze = false;
				// if(this.camera.position.z>=left_intersects[0].object.position.z)
					// this.camera.position.z-=40;
				// else
					// this.camera.position.z+=40;
			// }
			console.warn("intersected on	" + right_intersects[0].object.name + " " + right_intersects[0].object.gTypeId + " " + right_intersects[0].object.description);
			return;
		}

		var left_ray = new THREE.Ray(left, b_vector);
		var left_intersects = left_ray.intersectObjects(scene.children);
		if(left_intersects.length > 0 && left_intersects[0].distance < halfSize) {
			camera.position.z = left_intersects[0].point.z - halfSize - 1;
			controls.moveBackwardFreeze = true;
			controls.freeze = true;
			// if(left_intersects[0].object.gTypeId!='10'){
				// controls.moveBackwardFreeze = true;
				// controls.freeze = true;
			// }
			// else{
				// controls.freeze = false;
				// controls.moveBackwardFreeze = false;
				// if(this.camera.position.z>=left_intersects[0].object.position.z)
					// this.camera.position.z-=40;
				// else
					// this.camera.position.z+=40;
			// }

			console.warn("intersected on	" + left_intersects[0].object.name + " " + left_intersects[0].object.gTypeId + " " + left_intersects[0].object.description);
			return;
		}
		var right_ray = new THREE.Ray(right, b_vector);
		var right_intersects = right_ray.intersectObjects(scene.children);
		if(right_intersects.length > 0 && right_intersects[0].distance < halfSize) {
			camera.position.z = right_intersects[0].point.z - halfSize - 1;
			controls.moveBackwardFreeze = true;
			controls.freeze = true;
			// if(left_intersects[0].object.gTypeId!='10'){
				// controls.moveBackwardFreeze = true;
				// controls.freeze = true;
			// }
			// else{
				// controls.freeze = false;
				// controls.moveBackwardFreeze = false;
				// if(this.camera.position.z>=left_intersects[0].object.position.z)
					// this.camera.position.z-=40;
				// else
					// this.camera.position.z+=40;
			// }
			console.warn("intersected on	" + right_intersects[0].object.name + " " + right_intersects[0].object.gTypeId + " " + right_intersects[0].object.description);
			return;
		}

		var back_ray = new THREE.Ray(back, l_vector);
		var back_intersects = back_ray.intersectObjects(scene.children);
		if(back_intersects.length > 0 && back_intersects[0].distance < halfSize) {
			camera.position.x = back_intersects[0].point.x + halfSize + 1;
			controls.moveLeftFreeze = true;
			controls.freeze = true;
			// if(back_intersects[0].object.gTypeId!='10'){
			// controls.moveLeftFreeze = true;
			// controls.freeze = true;
			// }
			// else{
				// controls.freeze = false;
				// controls.moveLeftFreeze = false;
				// this.camera.position.x-=30;
			// }
			console.warn("intersected on	" + back_intersects[0].object.name + " " + back_intersects[0].object.gTypeId + " " + back_intersects[0].object.description);
			return;
		}
		var front_ray = new THREE.Ray(front, l_vector);
		var front_intersects = front_ray.intersectObjects(scene.children);
		if(front_intersects.length > 0 && front_intersects[0].distance < halfSize) {
			camera.position.x = front_intersects[0].point.x + halfSize + 1;
			controls.moveLeftFreeze = true;
			controls.freeze = true;
			// if(front_intersects[0].object.gTypeId!='10'){
			// controls.moveLeftFreeze = true;
			// controls.freeze = true;
			// }
			// else{
				// controls.freeze = false;
				// controls.moveLeftFreeze = false;
				// this.camera.position.x-=30;
			// }
			console.warn("intersected on	" + front_intersects[0].object.name + " " + front_intersects[0].object.gTypeId + " " + front_intersects[0].object.description);
			return;
		}

		var back_ray = new THREE.Ray(back, r_vector);
		var back_intersects = back_ray.intersectObjects(scene.children);
		if(back_intersects.length > 0 && back_intersects[0].distance < halfSize) {
			camera.position.x = back_intersects[0].point.x - halfSize - 1;
			controls.moveRightFreeze = true;
			controls.freeze = true;
			// if(back_intersects[0].object.gTypeId!='10'){
			// controls.moveRightFreeze = true;
			// controls.freeze = true;
			// }
			// else{
				// controls.freeze = false;
				// controls.moveRightFreeze = false;
				// this.camera.position.x+=30;
			// }
			console.warn("intersected on	" + back_intersects[0].object.name + " " + back_intersects[0].object.gTypeId + " " + back_intersects[0].object.description);
		}
		var front_ray = new THREE.Ray(front, r_vector);
		var front_intersects = front_ray.intersectObjects(scene.children);
		if(front_intersects.length > 0 && front_intersects[0].distance < halfSize) {
			camera.position.x = front_intersects[0].point.x - halfSize - 1;
			controls.moveRightFreeze = true;
			controls.freeze = true;
			// if(front_intersects[0].object.gTypeId!='10'){
			// controls.moveRightFreeze = true;
			// controls.freeze = true;
			// }
			// else{
				// controls.freeze = false;
				// controls.moveRightFreeze = false;
				// this.camera.position.x+=30;
			// }
			console.warn("intersected on	" + front_intersects[0].object.name + " " + front_intersects[0].object.gTypeId + " " + front_intersects[0].object.description);
		}
	};

	function getFacility(event) {

		// $(document).unbind('mousedown');
		if(house) {
			house.getFacility(event);
			// $('canvas').unbind('mousedown');
		}

	}

	//

	function animate() {

		requestAnimationFrame(animate);

		render();

	}

	function render() {
		var delta = clock.getDelta();
		controls.around(delta);
		// camera.position.copy(camera.position);
		if(house)
			house.update();

				// camera.position.x = 1400 ;
				// camera.position.z = 1400 ;
				// cameraTarget = new THREE.Vector3( 0, 0, 0 );
				// camera.lookAt( cameraTarget );
		cameraInsector();
		// plane.rotation.z = cube.rotation.y += ( targetRotation - cube.rotation.y ) * 0.05;
		renderer.render(scene, camera);

	}

	$('#save').click(function() {
		//alert(saveHouseUrl);
		saveHouse(saveHouseUrl);
	});
	function choiceFacility(goodsGeo) {
		// 				目前贴墙纸，无法实现换texture。只能通过增加mesh来实现？
		if(goodsGeo.gTypeId == "0901") {
			facility.paintSingle(facility.curMesh, goodsGeo.geometry, 1)
		} else
			house.loadFacility(goodsGeo);
		//facility.setFocus();
	}

	function buildHouse(hsId, roomStyles) {
		house = new House(camera, scene, stageSize);
		house.loadHouseStyle(hsId, roomStyles);
	}

	function saveHouse(saveHouseUrl) {
		house ? house.save(saveHouseUrl) : alert("please load house style first");

	};

	function loadHouse(h) {
		house = new House(camera, scene);
		house.load(h);
		// $('canvas').bind('click', function(event) {
		// getFocus(event, plane);
		// });
		// $('.stage canvas').bind('click',{stageSize:stageSize,objects:objects},locate);
		// alert(h);

	};


</script>
